package haizhi.fruitmall.service.impl;

import haizhi.fruitmall.dao.mapper.GoodsParamTypeMapper;
import haizhi.fruitmall.exception.ResultException;
import haizhi.fruitmall.pojo.form.GoodsParamTypeForm;
import haizhi.fruitmall.pojo.po.GoodsParamType;
import haizhi.fruitmall.pojo.po.GoodsParamTypeExample;
import haizhi.fruitmall.service.GoodsParamTypeService;
import haizhi.fruitmall.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by richa on 2017/9/11
 */


@Slf4j
@Service
@Transactional
public class GoodsParamTypeServiceImpl implements GoodsParamTypeService {

    @Autowired
    private GoodsParamTypeMapper goodsParamTypeMapper;

    @Override
    public void insert(GoodsParamTypeForm form) {
        if(BeanUtil.noNull(form.getTypePid())){
            GoodsParamType parent = this.getone(form.getTypePid());
            if(BeanUtil.isNull(parent))throw new ResultException("商品参数父级类型不存在！");
            if(BeanUtil.noNull(parent.getTypePid())) throw new ResultException("记录不是父级！");
        }
        GoodsParamType record = new GoodsParamType();
        BeanUtil.copyBean(form,record);
        record.setTypeId(BeanUtil.intId());
        goodsParamTypeMapper.insertSelective(record);
    }

    @Override
    public void update(GoodsParamTypeForm form){
        GoodsParamType record = this.getone(form.getTypeId());
        if(BeanUtil.isNull(record)){ throw new ResultException("记录不存在！"); }
        BeanUtil.copyBean(form,record);
        record.setUpdateTime(BeanUtil.newTime());
        goodsParamTypeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void delete(Integer id) {
        if(BeanUtil.isNull(this.getone(id))){ throw new ResultException("记录不存在！"); }
        goodsParamTypeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public GoodsParamType getone(Integer id) {
        if(BeanUtil.isNull(id)){ throw new ResultException("编号为空！");}
        return goodsParamTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<GoodsParamType> getall(GoodsParamTypeForm form) {
        GoodsParamTypeExample example = new GoodsParamTypeExample();
        GoodsParamTypeExample.Criteria criteria = example.createCriteria();
        if(BeanUtil.noEmpty(form.getTypeName())){
            criteria.andTypeNameLike(BeanUtil.isLike(form.getTypeName()));
        }
        if(BeanUtil.noEmpty(form.getTypeIds())){
            criteria.andTypeIdIn(form.getTypeIds());
        }
        return goodsParamTypeMapper.selectByExample(example);
    }

}
